package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.Archives;
import com.my.bean.ResultPage;
import com.my.bean.VoPage;
import com.my.service.ArchivesService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 销售管理的会员档案 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-09-12
 */
@Controller
@RequestMapping("/archives")
public class ArchivesController {

    @Autowired
    ArchivesService archivesService;
    //输出日志创建一个Logger对象
    Logger logger = Logger.getLogger(ArchivesController.class);

    /**
     * 添加销售管理的会员档案
     * @param archives
     * @return
     */
    @RequestMapping("/insertArchives")
    @ResponseBody
    public boolean insertArchives(Archives archives){
        boolean flag = archivesService.insert(archives);
        String msg = flag?"添加成功"+archives:"添加失败";
        logger.info(msg);
        return flag;

    }

    /**
     * 分页查询销售管理的会员档案所有
     * @param lin
     * @param data
     * @param voPage
     * @return
     */
    @RequestMapping("/selectArchives")
    @ResponseBody
    public ResultPage<Archives> selectArchives(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        ResultPage<Archives> list = archivesService.selectMyPage(map,voPage,admin);
        return list;
    }


    /**
     * 修改销售管理的会员档案
     * @param param
     * @return
     */
    @RequestMapping("/updateArchives")
    @ResponseBody
    public String updateArchives(String param){
        Archives archives = JSONObject.parseObject(param,Archives.class);
        if(archivesService.updateById(archives)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }


    /**
     * 删除销售管理的会员档案
     * @param id
     * @return
     */
    @RequestMapping("/deleteArchives")
    @ResponseBody
    public boolean deleteArchives(Long id){
        return archivesService.deleteById(id);
    }


    /**
     * 返回页面
     * @return
     */
    @RequestMapping("/archives")
    public String Archives(){
        return "archives";
    }



}
